package com.example.day12;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;

public class MainActivity extends AppCompatActivity {
    TextView text1;
    ImageView image;
    ProgressBar pro;
    Button button;
    String url;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        button=findViewById(R.id.button1);
        image=findViewById(R.id.image);
        text1=findViewById(R.id.text);
        pro=findViewById(R.id.pro);

     //  text1.setText(new HttpTools().httpReport("http://www.baidu.com/"));
         String url = "https://www.apiopen.top/createUserKey?";
        HashMap<String,String> aa = new HashMap();
        aa.put("appId", getPackageName());
        aa.put("passwd","09876");
        int pos=0;
        StringBuilder tempParams= new StringBuilder();
        if(aa!=null) {
            for (String key : aa.keySet()) {
                if (pos > 0) {
                    tempParams.append("&");
                }
                try {
                    tempParams.append(String.format("%s=%s", key, URLEncoder.encode(aa.get(key), "utf-8")));
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                }
                pos++;
            }
        }
            url = url + tempParams.toString();
        new MyAsyncTask(image).execute(url);

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new MyAsyncTask2(MainActivity.this,text1,pro).execute("17.txt");
//                url="http://n.sinaimg.cn/news/1_img/dfic/3ad618a7/106/w1024h682/20190423/rx1V-hvvuiyn6453816.jpg";
//                new MyAsyncTask(image).execute(url);
            }
        });
    }
}
